﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class RollCookie : MonoBehaviour
{
    [SerializeField]
    protected UISprite[] cookie;
    protected int index;
    [SerializeField]
    protected UILabel useNumberTx;
    [SerializeField]
    protected UILabel totalNumberTx;
    [SerializeField]
    protected UILabel totalNumberLB;
    
    public const string m_SelectIcon = "kuang_19";
    public const string m_NotSelectIcon = "kuang_20";

    public int Page 
    {
        get 
        {
            if(cookie == null)
            {
                return 0;
            }

            return cookie.Length;
        }
    }


    public void Move(int index) 
    {
        this.index = index;
        AllCookieSetDefulatSprite();
        cookie[index].spriteName = "";
    }

    public void MoveInterval(int index) 
    {
        //index = -index;
        ///Debug.Log("index: " + index);

        if (index < 0)
        {
            index = Math.Abs(index);
            index = index % cookie.Length;
        }
        else
        {
            if (index > cookie.Length)
                index = index % cookie.Length;

            index = (cookie.Length - index) % cookie.Length;
        }


        int newIndex = index;
        AllCookieSetDefulatSprite();
        cookie[newIndex].spriteName = m_SelectIcon;
    }

    public void MoveNext() 
    {
        if (cookie == null || cookie == null || cookie.Length == 0)
            return;
        
        
        if(index < cookie.Length - 1)
            index++;

        AllCookieSetDefulatSprite();
        cookie[index].spriteName = m_SelectIcon;
    }

    public void MovePrevious() 
    {
        if (cookie == null || cookie == null || cookie.Length == 0)
            return;

        if (index > 0)
            index--;

        AllCookieSetDefulatSprite();
        cookie[index].spriteName = m_SelectIcon;
    }

    private void AllCookieSetDefulatSprite() 
    {
        for (int i = 0; i < cookie.Length; i++)
        {
            cookie[i].spriteName = m_NotSelectIcon;
        }
    }

    public void SetNumber(int useItemCell, int maxItemCount) 
    {
        if (useNumberTx != null) 
        {
            useNumberTx.text = "(" + useItemCell.ToString();
        }

        if (totalNumberTx != null) 
        {
            totalNumberTx.text = maxItemCount.ToString() + ")";
        }

    }

    public void SetNumberColor(Color color) 
    {
        if (useNumberTx != null)
            useNumberTx.color = color;
        if (totalNumberTx != null)
            totalNumberTx.color = color;
        if (totalNumberLB != null)
            totalNumberLB.color = color;
    }
    
}
